// -----------------------------------------------------------------------------
// run_mapping.C
// Run the detector mapping stage for MUSIC detector data
// -----------------------------------------------------------------------------

// #include "HiMusicFittingTask.h"
// #include "FairRunAna.h"
// #include "FairRootManager.h"
// #include "FairFileSource.h"
// #include "FairRootFileSink.h"
// #include "TStopwatch.h"
#include <fairlogger/Logger.h>

void run_fit()
{

    // ======= Environment setup =======
    TString HiDir = "/home/user/lizy/HiRoot";
    TString inFile = HiDir + "/detectors/music/macro/mapped.root"; // 输入 ROOT 文件 (mapped data)
    TString outFile = "calibrated.root";                      // 输出文件 (after fitting)
    // TString parFile = "params.root";        // 参数文件

    // fair::Logger::SetConsoleSeverity("debug"); //New version
    // fair::Logger::SetConsoleSeverity("state"); //New version 
    fair::Logger::SetConsoleColor(true);
    // ======= FairRunAna: main driver =======
    FairRunAna* fRun = new FairRunAna();
    FairFileSource* fFileSource = new FairFileSource(inFile);
    fRun->SetSource(fFileSource);
    fRun->SetSink(std::make_unique<FairRootFileSink>(outFile));
    fRun->SetUseFairLinks(kFALSE);

    // // ======= Add your Mapping Task =======
    auto *fitTask = new HiMusicFittingTask();
    fRun->AddTask(fitTask);

    // // ======= Initialize and Run =======
    fRun->Init();
    fRun->Run();

    // ======= Done =======
    std::cout << "  Fitting finished successfully!" << std::endl;
    std::cout << "   Input : " << inFile << std::endl;
    std::cout << "   Output: " << outFile << std::endl;
}
